<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\DB;

class BannerController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //查询横幅
        $banner = DB::table('banner')
            ->select('id', 'issue', 'date_issue', 'price', 'buy_time', 'buyer')
            ->where(function ($query) {
                $query->orWhere('issue', 'like', '%' . @$_GET['search'] . '%');
                $query->orWhere('date_issue', 'like', '%' . @$_GET['search'] . '%');
                $query->orWhere('price', 'like', '%' . @$_GET['search'] . '%');
                $query->orWhere('buy_time', 'like', '%' . @$_GET['search'] . '%');
                $query->orWhere('buyer', 'like', '%' . @$_GET['search'] . '%');
            })
            ->paginate(31)
            ->withPath('?search=' . @$_GET['search'] . '');

        return view('admin.banner.index', ['banner' => $banner]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //查询横幅信息
        $banner_info = DB::table('banner')
            ->select('id', 'issue', 'date_issue', 'price', 'buy_time', 'buyer')
            ->where('id',$id)
            ->first();

        return view('admin.banner.edit',['banner_info' => $banner_info]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //验证表单
        $this->validate($request, [
            'price' => ['required','numeric'],
        ]);

        //更新购买成功到数据库
        DB::table('banner')
            ->where('id',$id)
            ->update([
                'price' => $request->price
            ]);

        return response()->json(['message' => '编辑成功。', 'success' => 'true']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function record()
    {
        //查询用户推广数据
        $banner_record = DB::table('banner_record')
            ->select('id', 'issue', 'date_issue', 'consume', 'buy_time', 'buyer')
            ->where(function ($query) {
                $query->orWhere('issue', 'like', '%'.@$_GET['search'].'%');
                $query->orWhere('date_issue', 'like', '%'.@$_GET['search'].'%');
                $query->orWhere('consume', 'like', '%'.@$_GET['search'].'%');
                $query->orWhere('buy_time', 'like', '%'.@$_GET['search'].'%');
                $query->orWhere('buyer', 'like', '%'.@$_GET['search'].'%');
            })
            ->orderBy('id', 'desc')
            ->paginate(18)
            ->withPath('?search=' . @$_GET['search'] . '');

        return view('admin.banner.record',['banner_record'=>$banner_record]);
    }

}
